<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$
**********************************************/
if (!defined('CPG_NUKE')) { exit; }
define('HEADER_OPEN', true);

// temporary patch
if ('HEAD' === $_SERVER['REQUEST_METHOD']) {
	HttpHeader::flush();
	exit;
}

# 1-2 queries
function online() {
	global $userinfo, $prefix, $db, $module_title, $SESS, $mainindex;
	if ($SESS->dbupdate) {
		$url = URL::uri();
		$uname = $SESS->sess_id;
		$guest = 1;
		if (is_user()) {
			$uname = $userinfo['username'];
			$guest = 0;
		} elseif (SEARCHBOT) {
			$uname = SEARCHBOT;
			$guest = 3;
		}
		if (is_admin()) {
			global $CLASS;
			if ($guest == 1) { $uname = $CLASS['member']->admin['aid']; }
			$guest = 2;
			if (defined('ADMIN_PAGES')) $url = $mainindex;
		}
		if (empty($uname)) return; # something screwey
		$ctime = time();
		$custom_title = $module_title ? $module_title : _HOME;
		$url = str_replace('&', '&amp;', $url);
		$tbl = $db->TBL->session;
		$data = array(
			'time'   => $ctime,
			'module' => $custom_title,
			'url'    => $url,
			'guest'  => $guest,
		);
		if ($tbl->count(array('uname'=>$uname))) {
			$tbl->update($data, array('uname'=>$uname));
		} else {
			$data['module'] = $db->quote($custom_title);
			$data['url']    = $db->quote($url);
			$data['uname']  = $db->quote($uname);
			$data['host_addr'] = $db->escapeBinary(Dragonfly_Net::ipn());
			$db->query('INSERT INTO '.$prefix.'_session ('.implode(', ', array_keys($data)).') VALUES ('.implode(', ', $data).')');
		}
	}
}

function head() {
	global $BASEHREF, $METATAGS, $sitename, $userinfo, $Blocks, $home, $cpgtpl,
		$bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2,
		$pagetitle, $modheader, $MAIN_CFG, $CPG_SESS, $Module, $CLASS;
	define('THEME_PATH', 'themes/' .$CPG_SESS['theme'] .'/');
	header('imagetoolbar: no');
	$header = '';
	foreach ($METATAGS as $name => $content) {
		$header .= '<meta name="'.$name.'" content="'.$content.'" />'."\n";
	}
//	$header .= '<script type="text/javascript" src="includes/javascript/MM_funcs.js"></script>'."\n\n";
	$header .= $modheader.'
<link rel="copyright" href="'.URL::index('credits').'" title="Copyrights" />
<link rel="author" href="'.URL::index('Members_List').'" title="'._Members_ListLANG.'" />
';
if (!defined('ADMIN_PAGES')) {
	$header .= '<link rel="canonical" href="'.URL::canonical().'" />
';
}
/*
	$languages = lang_selectbox('', '', false, true);
	for ($i=0; $i < sizeof($languages); $i++) {
		if ($languages[$i] != '') {
			$header .= '<link rel="alternate" type="text/html" href="?newlang='.$languages[$i].'" hreflang="'.get_langcode($languages[$i]).'" lang="'.get_langcode($languages[$i]).'" title="Version '.ucfirst($languages[$i]).'" />'."\n";
		}
	}
*/
	if ($Module->name != 'Private_Messages' && is_user() && is_active('Private_Messages') && $userinfo['user_popup_pm'] && $userinfo['user_new_privmsg']) {
		$header .= '
<script type="text/javascript">
<!--
	window.open(\''.URL::index('Private_Messages&file=index&mode=newpm', false).'\', \'\', \'height=150,resizable=yes,width=400\');
//-->
</script>';
	}
	$message = $MAIN_CFG['global']['maintenance'] ? '<strong>'._SYS_MESSAGE.'</strong><br />'._SYS_MAINTENANCE : '';
	if (is_admin() && $CLASS['member']->demo) {
		$message .= ($message == '') ? '<strong>'._SYS_MESSAGES.'</strong>' : '<br />';
		$message .= '<br />'._SYS_DEMO;
	}

	$adminmenuitems = $admincssmenuitems = false;
	if (!$home) {
		if (defined('ADMIN_PAGES') && is_admin() && !(isset($_GET['op']) && $_GET['op'] == 'logout')) {
			require_once('includes/classes/cpg_adminmenu.php');
			$CLASS['adminmenu']->display();
			if ($MAIN_CFG->global->admingraphic & 8) $adminmenuitems = $CLASS['adminmenu']->display('all', 'jsmenu');
			$admincssmenuitems = $MAIN_CFG->global->admingraphic & 4;
		}
	}

	CSS::add(THEME_PATH. 'style/style.css', 'screen');
	JS::add('includes/javascript/poodle.js');
	if (defined('ADMIN_PAGES')) {
		CSS::add('themes/default/style/admin.css');
		if ($MAIN_CFG['global']['admin_help']) {
			JS::add('includes/javascript/infobox.js');
		}
	}

	$Blocks = new Blocks($Module->mid);
	$Blocks->prepare(Blocks::LEFT);
	$Blocks->prepare(Blocks::CENTER);
	$Blocks->prepare(Blocks::RIGHT);
	$Blocks->prepare(Blocks::DOWN);

	$cpgtpl->assign_vars(array(
		'BASEHREF'     => $BASEHREF,
		'I18N'		     => 'enctype="multipart/form-data" accept-charset="utf-8"',
		'IMPORTANT_MESSAGE' => $message,
		'PAGE_TITLE'   => ($home || isset($ownpagetitle[$Module->name]))?'':$pagetitle,
		'S_TEXTDIR'	   => _TEXT_DIR,
		'S_LANGCODE'	 => _BROWSER_LANGCODE,
		'S_HEADER_TAGS'=> $header,
		'S_SITENAME'   => $sitename,
		'S_PAGETITLE'  => !empty($pagetitle) ? strip_tags($pagetitle) : '',
		'S_DELIM'      => _BC_DELIM,
		/* iSCSI, HTTP or FTP allowed */
		'S_FILESERVER' => '',
		'S_ADMIN_MENU' => $adminmenuitems,
		'S_ADMIN_CSSMENU' => $admincssmenuitems,
		'S_MAIN_MENU' => $MAIN_CFG->global->admingraphic & 4,
		'S_BLOCK_FRAMES' => intval($MAIN_CFG['global']['block_frames']),
		'B_SIDE_LEFT'  => $Module->sides & Blocks::LEFT,
		'B_SIDE_CENTER'=> $Module->sides & Blocks::CENTER,
		'B_SIDE_RIGHT' => $Module->sides & Blocks::RIGHT,
		'B_SIDE_DOWN'  => $Module->sides & Blocks::DOWN,
		'B_PAGETITLE'  => 1 < strlen(strip_tags($pagetitle)),
		'S_IS_ADMIN'   => is_admin(),
		'S_CAN_ADMIN'  => can_admin(),
		'IS_ADMIN'     => is_admin(),
		'CAN_ADMIN'    => can_admin(),
		'ADMIN_PAGES'  => defined('ADMIN_PAGES')
	));

	if (!defined('_BROWSER_LANGCODE')) { define('_BROWSER_LANGCODE', _LANGCODE); }
	if (!defined('_CHARSET')) { define('_CHARSET', 'UTF-8'); }

	include_once(THEME_PATH .'theme.php');
	if (!defined('THEME_VERSION')) { define('THEME_VERSION', '9.0'); }

	themeheader();
	unset($modheader);
}
//if (300 < $_SESSION['SECURITY']['status']) { online(); }
if (empty($_SESSION['SECURITY']['banned'])) { online(); }
global $home, $cpgtpl, $Module;
head();
if (!defined('ADMIN_PAGES')) {
	require_once('includes/counter.php');  # 2-3 queries
	require_once('includes/functions/messagebox.php');
}
$cpgtpl->set_filenames(array('cpgheader' => 'header.html'));
$cpgtpl->display('cpgheader');
